package com.billsharing.server;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class Report
 */
@WebServlet("/MakePayment")
public class MakePayment extends HttpServlet {
	private static final long serialVersionUID = 1L;
    int uid;
    String friendEmail;
    double amount;
    String paymentType;
    
    /**
     * @see HttpServlet#HttpServlet()
     */
    public MakePayment() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		PrintWriter out=response.getWriter();
		uid = Integer.parseInt(request.getParameter("uid"));
		friendEmail = request.getParameter("friendEmail");
		friendEmail = friendEmail.replaceAll("\\s+","");
		paymentType = request.getParameter("payment");
		paymentType = paymentType.replaceAll("\\s+","");
		amount = Double.parseDouble(request.getParameter("amount"));
	if(paymentType.equals("made"))
	{
		System.out.println(true);
		User payer = new User(uid);
		payer.reportPayment(friendEmail, amount);	
		out.print("true");
	}
	
	else if(paymentType.equals("recieved"))
	{
		System.out.println("recieved true ");
		User payer = new User(friendEmail);
		System.out.println(payer.getUid());
	//	int payerUid = payer.getUid();
		
		User payee = new User(uid);
		String payeeEmail = payee.getEmail();
		
		payer.reportPayment(payeeEmail, amount);

		
		out.print("true");
		
	}
		
		System.out.println(paymentType+uid+friendEmail+amount);
	}

}
